package com.xgc.oauth.config;

import com.xgc.oauth.custom.OAuth2Reaml;
import jakarta.servlet.ServletRequest;
import jakarta.servlet.ServletResponse;
import org.apache.shiro.authc.BearerToken;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.spring.LifecycleBeanPostProcessor;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;

@Configuration
public class ShiroConfig {

    /**
     * 用于管理shiro组件的生命周期
     *
     * @return
     */
    @Bean("lifecycleBeanPostProcessor")
    public static LifecycleBeanPostProcessor lifecycleBeanPostProcessor() {
        return new LifecycleBeanPostProcessor();
    }

    @Bean("jdbcRealm")
    public OAuth2Reaml oAuth2Reaml() {
        OAuth2Reaml oAuth2Reaml = new OAuth2Reaml();
//        oAuth2Reaml.setAuthenticationTokenClass(BearerToken.class);
        return oAuth2Reaml;
    }

    @Bean("securityManager")
    public SecurityManager securityManager(OAuth2Reaml oAuth2Reaml) {
        return new DefaultWebSecurityManager(oAuth2Reaml);
    }

    /**
     *
     * <p>其中 filterMap.put("/**", "authcBearer[options]") 的含义为：</p>
     * 设置需要Bearer方式认证的url,并设置允许跨域访问，跨域的url直接放行,最终调用的方法：{@link org.apache.shiro.web.filter.authc.HttpAuthenticationFilter#isAccessAllowed(ServletRequest, ServletResponse, Object)}
     * <p>也可以自定义过滤器然后重写isAccessAllowed方法也能达到一样的效果</p>
     */
    @Bean("shiroFilter")
    public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) {
        ShiroFilterFactoryBean shiroFilter = new ShiroFilterFactoryBean();
        Map<String, String> filterMap = new LinkedHashMap<>();
        //设置不需要认证的url
        filterMap.put("/oauth/login", "anon");
        //设置需要Bearer方式认证的url,并设置允许跨域访问，跨域的url直接放行，
        filterMap.put("/**", "authcBearer[options]");
        shiroFilter.setFilterChainDefinitionMap(filterMap);
        shiroFilter.setSecurityManager(securityManager);
        //设置无状态服务（禁用会话）
        shiroFilter.setGlobalFilters(Arrays.asList("noSessionCreation"));
        return shiroFilter;
    }


    //开启shiro注解
    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator(){
        DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator=new DefaultAdvisorAutoProxyCreator();
        defaultAdvisorAutoProxyCreator.setProxyTargetClass(true);
        return defaultAdvisorAutoProxyCreator;
    }

    //开启shiro注解
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor advisor = new AuthorizationAttributeSourceAdvisor();
        advisor.setSecurityManager(securityManager);
        return advisor;
    }

}
